# libc/mcs51 Makefile

VPATH  = @srcdir@
srcdir = @srcdir@

top_srcdir   = @top_srcdir@
top_builddir = @top_builddir@

LIB_TYPE     = @LIB_TYPE@
SAS = $(top_builddir)/bin/sdas8051
SCC = $(top_builddir)/bin/sdcc
SDAR = $(top_builddir)/bin/sdar

# override PORTDIR defined by super (parent) makefile
override PORTDIR = ../build/$(PORT)

OBJ = crtstart.rel crtxinit.rel crtxclear.rel crtclear.rel \
      crtpagesfr.rel crtbank.rel crtcall.rel \
      crtxstack.rel crtxpush.rel crtxpushr0.rel crtxpop.rel crtxpopr0.rel \
      gptr_cmp.rel \
      atomic_flag_test_and_set.rel atomic_flag_clear.rel __sdcc_atomic_maybe_rollback.rel

LIB = mcs51.lib

CC = $(SCC)
AS = $(SAS)
ASFLAGS = -plosgff

CFLAGS = -I$(top_srcdir)/device/include --std-c23

all: $(PORTDIR)/$(LIB)

$(PORTDIR)/$(LIB): $(OBJ) Makefile
ifeq ($(LIB_TYPE), SDCCLIB)
	rm -f $@; \
	$(top_builddir)/bin/sdcclib -a $@ $(OBJ)
else
  ifeq ($(LIB_TYPE), AR)
	$(SDAR) -rcSD $@ $(OBJ)
  else
    ifeq ($(LIB_TYPE), RANLIB)
	$(SDAR) -rcD $@ $(OBJ)
    else
	rm -f $@
	for i in $(basename $(OBJ)); do echo $$i >>$@; done
	cp $(OBJ) $(PORTDIR)
    endif
  endif
endif

%.rel: %.c
	$(CC) $(CFLAGS) -c $<

%.rel: %.asm
	@# TODO: asx8051 should place it\'s output in the current dir
	test $(srcdir) = . || cp $< .
	-$(AS) $(ASFLAGS) $(notdir $<)
	test $(srcdir) = . || rm $(notdir $<)

clean:
	rm -f *.rel *.sym *.lst *~ $(CLEANSPEC) *.dump* *.lib

distclean: clean
	rm -r Makefile

Makefile: $(srcdir)/Makefile.in
	cd $(top_builddir); ./config.status device/lib/mcs51/Makefile
